﻿using System;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using System.Web.Mvc;

namespace ElValWeb.ValidatorClientAdapters {
    class RegularExpressionValidatorClientAdapter : BaseClientAdapter<RegexValidatorData> {

        public RegularExpressionValidatorClientAdapter(RegexValidatorData validatorData) : base(validatorData) {
            
        }

        public override IEnumerable<ModelClientValidationRule> GetClientRules(Type ownerType) {
            return new[] {Negate( new ModelClientValidationRegexRule(ValidatorData.GetMessageTemplate(), GetPattern()), ValidatorData.Negated) };

        }

        public string GetPattern() {
            if (!string.IsNullOrEmpty(ValidatorData.Pattern)) {
                return ValidatorData.Pattern;
            }
            return ResourceStringLoader.LoadString(ValidatorData.PatternResourceTypeName, ValidatorData.PatternResourceName, ValidatorData.PatternResourceType.Assembly);
        }
    }

}
